#install.packages("pacman")
library(pacman)
p_load("tidyverse","fastDummies","rio")
#additional packages and functions
# version 0.1.0.9000 used

#functions--------
zero1 <- function(x, minx=NA, maxx=NA){
  res <- NA
  if(is.na(minx)) res <- (x - min(x,na.rm=T))/(max(x,na.rm=T) -min(x,na.rm=T))
  if(!is.na(minx)) res <- (x - minx)/(maxx -minx)
  res
}

#import data ------------
gss2006 <- import("DataverseData/GSS_panel06w123_R6a - Stata.dta") #specify the folder where your data is stored
gss2008 <- import("DataverseData/GSS_panel08w123_R6 - stata.dta") #specify the folder where your data is stored
gss2010 <- import("DataverseData/GSS_panel2010w123_R6 - stata.dta") #specify the folder where your data is stored
#2006 GSS------------------------
gss2006$auth1 <- zero1(rowMeans(data.frame(6-gss2006$obey_1,gss2006$thnkself_1),na.rm=T))
gss2006$auth2 <- zero1(rowMeans(data.frame(6-gss2006$obey_2,gss2006$thnkself_2),na.rm=T))
gss2006$auth3 <- zero1(rowMeans(data.frame(6-gss2006$obey_3,gss2006$thnkself_3),na.rm=T))

#economic attitudes
gss2006$econatts1 <- zero1(rowMeans(data.frame(gss2006$helppoor_1,gss2006$helpsick_1,gss2006$eqwlth_1, gss2006$helpnot_1),na.rm=T))
gss2006$econatts2 <- zero1(rowMeans(data.frame(gss2006$helppoor_2,gss2006$helpsick_2,gss2006$eqwlth_2, gss2006$helpnot_2),na.rm=T))
gss2006$econatts3 <- zero1(rowMeans(data.frame(gss2006$helppoor_3,gss2006$helpsick_3,gss2006$eqwlth_3, gss2006$helpnot_3),na.rm=T))

#LBTQ index. Note that homosex_1 was not preregistered
gss2006$homo1 <- rowMeans(data.frame(zero1(gss2006$spkhomo_1),zero1(gss2006$colhomo_1),zero1(3-gss2006$libhomo_1),na.rm=T))
gss2006$homo2 <- rowMeans(data.frame(zero1(gss2006$spkhomo_2),zero1(gss2006$colhomo_2),zero1(3-gss2006$libhomo_2),na.rm=T))
gss2006$homo3 <- rowMeans(data.frame(zero1(gss2006$spkhomo_3),zero1(gss2006$colhomo_3),zero1(3-gss2006$libhomo_3),na.rm=T))
#Abortion
gss2006$abortion1 <- rowMeans(data.frame(zero1(gss2006$abany_1),zero1(gss2006$abrape_1),zero1(gss2006$abdefect_1),zero1(gss2006$abhlth_1),zero1(gss2006$abnomore_1),zero1(gss2006$abpoor_1),zero1(gss2006$absingle_1),na.rm=T))
gss2006$abortion2 <- rowMeans(data.frame(zero1(gss2006$abany_2),zero1(gss2006$abrape_2),zero1(gss2006$abdefect_2),zero1(gss2006$abhlth_2),zero1(gss2006$abnomore_2),zero1(gss2006$abpoor_2),zero1(gss2006$absingle_2),na.rm=T))
gss2006$abortion3 <- rowMeans(data.frame(zero1(gss2006$abany_3),zero1(gss2006$abrape_3),zero1(gss2006$abdefect_3),zero1(gss2006$abhlth_3),zero1(gss2006$abnomore_3),zero1(gss2006$abpoor_3),zero1(gss2006$absingle_3),na.rm=T))
#select variables
gss2006_s <- dplyr::select(gss2006,abortion1,abortion2,abortion3,econatts1,econatts2,econatts3,homo1,homo2,homo3,partyid_1, partyid_2, partyid_3, polviews_1, polviews_2, polviews_3,  auth1,auth2,auth3, wtpan123, sex_1, racecen1_1, degree_1, age_1,abany_1,abrape_1,abdefect_1,abhlth_1,abnomore_1,abpoor_1,absingle_1,abany_2,abrape_2,abdefect_2,abhlth_2,abnomore_2,abpoor_2,absingle_2,abany_3,abrape_3,abdefect_3,abhlth_3,abnomore_3,abpoor_3,absingle_3,obey_1,obey_2,obey_3,thnkself_1,thnkself_2,thnkself_3,age_1,age_2,age_3,sex_1,sex_2,sex_3,degree_1,degree_2,degree_3,race_1,race_2,race_3,rincom06_1)
gss2006_s$year <- 2006

#2008 GSS------------------------
gss2008$auth1 <- zero1(rowMeans(data.frame(6-gss2008$obey_1,gss2008$thnkself_1),na.rm=T))
gss2008$auth2 <- zero1(rowMeans(data.frame(6-gss2008$obey_2,gss2008$thnkself_2),na.rm=T))
gss2008$auth3 <- zero1(rowMeans(data.frame(6-gss2008$obey_3,gss2008$thnkself_3),na.rm=T))

#economic attitudes
gss2008$econatts1 <- zero1(rowMeans(data.frame(gss2008$helppoor_1,gss2008$helpsick_1,gss2008$eqwlth_1, gss2008$helpnot_1),na.rm=T))
gss2008$econatts2 <- zero1(rowMeans(data.frame(gss2008$helppoor_2,gss2008$helpsick_2,gss2008$eqwlth_2, gss2008$helpnot_2),na.rm=T))
gss2008$econatts3 <- zero1(rowMeans(data.frame(gss2008$helppoor_3,gss2008$helpsick_3,gss2008$eqwlth_3, gss2008$helpnot_3),na.rm=T))

#LBTQ index. Note that homosex_1 was not preregistered
gss2008$homo1 <- rowMeans(data.frame(zero1(gss2008$spkhomo_1),zero1(gss2008$colhomo_1),zero1(3-gss2008$libhomo_1),na.rm=T))
gss2008$homo2 <- rowMeans(data.frame(zero1(gss2008$spkhomo_2),zero1(gss2008$colhomo_2),zero1(3-gss2008$libhomo_2),na.rm=T))
gss2008$homo3 <- rowMeans(data.frame(zero1(gss2008$spkhomo_3),zero1(gss2008$colhomo_3),zero1(3-gss2008$libhomo_3),na.rm=T))

#abortion
gss2008$abortion1 <- rowMeans(data.frame(zero1(gss2008$abany_1),zero1(gss2008$abrape_1),zero1(gss2008$abdefect_1),zero1(gss2008$abhlth_1),zero1(gss2008$abnomore_1),zero1(gss2008$abpoor_1),zero1(gss2008$absingle_1),na.rm=T))
gss2008$abortion2 <- rowMeans(data.frame(zero1(gss2008$abany_2),zero1(gss2008$abrape_2),zero1(gss2008$abdefect_2),zero1(gss2008$abhlth_2),zero1(gss2008$abnomore_2),zero1(gss2008$abpoor_2),zero1(gss2008$absingle_2),na.rm=T))
gss2008$abortion3 <- rowMeans(data.frame(zero1(gss2008$abany_3),zero1(gss2008$abrape_3),zero1(gss2008$abdefect_3),zero1(gss2008$abhlth_3),zero1(gss2008$abnomore_3),zero1(gss2008$abpoor_3),zero1(gss2008$absingle_3),na.rm=T))

#select variables
gss2008_s <- dplyr::select(gss2008,abortion1,abortion2,abortion3,econatts1,econatts2,econatts3,homo1,homo2,homo3,partyid_1, partyid_2, partyid_3, polviews_1, polviews_2, polviews_3,  auth1,auth2,auth3, wtpan123, sex_1, racecen1_1, degree_1, age_1,abany_1,abrape_1,abdefect_1,abhlth_1,abnomore_1,abpoor_1,absingle_1,abany_2,abrape_2,abdefect_2,abhlth_2,abnomore_2,abpoor_2,absingle_2,abany_3,abrape_3,abdefect_3,abhlth_3,abnomore_3,abpoor_3,absingle_3,obey_1,obey_2,obey_3,thnkself_1,thnkself_2,thnkself_3,age_1,age_2,age_3,sex_1,sex_2,sex_3,degree_1,degree_2,degree_3,race_1,race_2,race_3,rincom06_1)

gss2008_s$year <- 2008
#2010 GSS------------------------
gss2010$auth1 <- zero1(rowMeans(data.frame(6-gss2010$obey_1,gss2010$thnkself_1),na.rm=T))
gss2010$auth2 <- zero1(rowMeans(data.frame(6-gss2010$obey_2,gss2010$thnkself_2),na.rm=T))
gss2010$auth3 <- zero1(rowMeans(data.frame(6-gss2010$obey_3,gss2010$thnkself_3),na.rm=T))

#economic
gss2010$econatts1 <- zero1(rowMeans(data.frame(gss2010$helppoor_1,gss2010$helpsick_1,gss2010$eqwlth_1, gss2010$helpnot_1),na.rm=T))
gss2010$econatts2 <- zero1(rowMeans(data.frame(gss2010$helppoor_2,gss2010$helpsick_2,gss2010$eqwlth_2, gss2010$helpnot_2),na.rm=T))
gss2010$econatts3 <- zero1(rowMeans(data.frame(gss2010$helppoor_3,gss2010$helpsick_3,gss2010$eqwlth_3, gss2010$helpnot_3),na.rm=T))

#abortion
gss2010$abortion1 <- rowMeans(data.frame(zero1(gss2010$abany_1),zero1(gss2010$abrape_1),zero1(gss2010$abdefect_1),zero1(gss2010$abhlth_1),zero1(gss2010$abnomore_1),zero1(gss2010$abpoor_1),zero1(gss2010$absingle_1),na.rm=T))
gss2010$abortion2 <- rowMeans(data.frame(zero1(gss2010$abany_2),zero1(gss2010$abrape_2),zero1(gss2010$abdefect_2),zero1(gss2010$abhlth_2),zero1(gss2010$abnomore_2),zero1(gss2010$abpoor_2),zero1(gss2010$absingle_2),na.rm=T))
gss2010$abortion3 <- rowMeans(data.frame(zero1(gss2010$abany_3),zero1(gss2010$abrape_3),zero1(gss2010$abdefect_3),zero1(gss2010$abhlth_3),zero1(gss2010$abnomore_3),zero1(gss2010$abpoor_3),zero1(gss2010$absingle_3),na.rm=T))

#lbtq
gss2010$homo1 <- rowMeans(data.frame(zero1(gss2010$spkhomo_1),zero1(gss2010$colhomo_1),zero1(3-gss2010$libhomo_1),na.rm=T))
gss2010$homo2 <- rowMeans(data.frame(zero1(gss2010$spkhomo_2),zero1(gss2010$colhomo_2),zero1(3-gss2010$libhomo_2),na.rm=T))
gss2010$homo3 <- rowMeans(data.frame(zero1(gss2010$spkhomo_3),zero1(gss2010$colhomo_3),zero1(3-gss2010$libhomo_3),na.rm=T))
#rename weight variable
gss2010$wtpan123<-gss2010$WTPAN123

#select variables
gss2010_s <- dplyr::select(gss2010,abortion1,abortion2,abortion3,econatts1,econatts2,econatts3,homo1,homo2,homo3,partyid_1, partyid_2, partyid_3, polviews_1, polviews_2, polviews_3,  auth1,auth2,auth3, wtpan123, sex_1, racecen1_1, degree_1, age_1,abany_1,abrape_1,abdefect_1,abhlth_1,abnomore_1,abpoor_1,absingle_1,abany_2,abrape_2,abdefect_2,abhlth_2,abnomore_2,abpoor_2,absingle_2,abany_3,abrape_3,abdefect_3,abhlth_3,abnomore_3,abpoor_3,absingle_3,obey_1,obey_2,obey_3,thnkself_1,thnkself_2,thnkself_3,age_1,age_2,age_3,sex_1,sex_2,sex_3,degree_1,degree_2,degree_3,race_1,race_2,race_3,rincom06_1)
gss2010_s$year<-2010

#Combine all variables--------------
gssall <- rbind(gss2006_s,gss2008_s,gss2010_s)
#exclude missing on the weights --> complete cases in the panel
gssall <- gssall[complete.cases(gssall$wtpan123),]

#recode variables to range from 0 to 1
gssall$partyid_1<-zero1(gssall$partyid_1)
gssall$partyid_2<-zero1(gssall$partyid_2)
gssall$partyid_3<-zero1(gssall$partyid_3)

gssall$polviews_1<-zero1(gssall$polviews_1)
gssall$polviews_2<-zero1(gssall$polviews_2)
gssall$polviews_3<-zero1(gssall$polviews_3)

#some basic coding--------------
all <- gssall
all$year2010 <- ifelse(all$year==2010,1,0)
all$year2008 <- ifelse(all$year==2008,1,0)
all$white1 <- ifelse(all$race_1==1,1,0)
all$white2 <- ifelse(all$race_2==1,1,0)
all <- dummy_cols(all,select_columns = "degree_1")
all <- dummy_cols(all,select_columns = "race_1")
all$rincome4 <- as.numeric(gtools::quantcut(all$rincom06_1,q = seq(0, 1, by=0.25),na.rm = T))
all$rincome4[is.na(all$rincome4)]=0
all <- dummy_cols(all,select_columns = "rincome4")
all$id<-1:nrow(all)
all$edu01<-ifelse(all$degree_1>2, 1,0)

#save data
save(gss2006,file="Data/gssall_2006.RData")
save(gss2008,file="Data/gssall_2008.RData")
save(gss2010,file="Data/gssall_2010.RData")
save(all,file="Data/gssall.RData")
